/*
 * @Description: 
 * @Author: Aroma
 * @Date: 2024-09-29 09:43:18
 * @LastEditors: Aroma
 * @LastEditTime: 2024-09-29 17:17:56
 * @别瞎看: 你个无脑牛马
 */

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * Test02TreeSet
 */
public class Test02TreeSet {

    public static void main(String[] args) {
        //实例化一个集合对象
        Set<Integer> set=new TreeSet<>();
        //添加元素
        set.add(1);
        set.add(7);
        set.add(3);
        set.add(5);
        //遍历
        for (Integer integer : set) {
            System.out.println(integer);
        }
        //实例化一个集合对象
        Set<Student> setStudent=new TreeSet<>();
        //没有实例相关自然排序接口  所以这里会报错
        setStudent.add(new Student("张三", 20));
        setStudent.add(new Student("李四", 18));
        setStudent.add(new Student("王五", 19));
        setStudent.add(new Student("赵六", 21));
        //遍历
        for (Student s: setStudent) {
            System.out.println(s);
        }
        //1.自定义比较器实现比较规则
        //注意：源码中的重写的方法要注释掉
        //注意：源码中的实现的接口要注释掉
        //准备自定义比较器对象
        Comparator<Student> comp = new Comparator<Student>(){
            //匿名内部类
            //重写compare方法
            @Override
            public int compare(Student o1, Student o2) {
                int result = 0;
                //比较规则:先按age升序 再按name降序
                result = o1.getAge()-o2.getAge();
                if(result==0){
                    result = o2.getName().compareTo(o1.getName());
                }
                return result;
            }
        };
        //2.实例化一个TreeSet传入自定义比较器
        Set<Student> setStudent2=new TreeSet<>(comp);
        setStudent2.add(new Student("张三", 20));
        setStudent2.add(new Student("李四", 21));
        setStudent2.add(new Student("王五", 19));
        setStudent2.add(new Student("赵六", 21));
        //遍历
        for (Student s: setStudent2) {
            System.out.println(s);
        }
    }
}